const axios = require("axios");

// 重试配置
const MAX_RETRIES = 3;
const RETRY_DELAY_MS = 30000; // 20秒

/**
 * 带重试机制的请求函数
 * @param {string} url - 请求URL
 * @param {number} retryCount - 当前重试次数
 * @returns {Promise}
 */
async function executeWithRetry(url, retryCount = 0) {
  try {
    const response = await axios.post(url);
    return response;
  } catch (error) {
    console.error(`[${new Date().toISOString()}] 请求失败:`, error.message);

    if (retryCount < MAX_RETRIES) {
      console.log(`[${new Date().toISOString()}] ${RETRY_DELAY_MS/1000}秒后重试...`);
      await new Promise(resolve => setTimeout(resolve, RETRY_DELAY_MS));
      return executeWithRetry(url, retryCount + 1);
    } else {
      console.error(`[${new Date().toISOString()}] 达到最大重试次数(${MAX_RETRIES})，请求失败`);
      throw error;
    }
  }
}

module.exports = {
    executeWithRetry
}